<?php

class ProjectUserForm extends CFormModel
{
    public $username;
    public $role;
    public $project;
    private $_user;
    
    public function rules()
    {
        return array(
            // username and password are required
            array('username, role', 'required'),
            // username needs to be checked for existence
            array('username', 'exist', 'className'=>'User'),
            array('username', 'verify'),
        );
    }
    
    public function verify($attribute, $params)
    {
        if(!$this->hasErrors())
        {
            //$user = User::model()->find('LOWER(username) = ?', array(strtolower($this->username)));
            $user = User::model()->findByAttributes(array('username'=>$this->username));
            if ($this->project->isUserInProject($user)) {
                $this->addError('username', 'This user has already been added to the project');
            } else {
                $this->_user = $user;
            }
        }
    }
    
    public function assign()
    {
        if($this->_user instanceof User) {
            //assign the user, in the specified role, to the project
            $this->project->assignUser($this->_user->id, $this->role);  
            //add the association, along with the RBAC biz rule, to our RBAC hierarchy
            $auth = Yii::app()->authManager; 
            $bizRule='return isset($params["project"]) && $params["project"]->allowCurrentUser("'.$this->role.'");';  
            $auth->assign($this->role,$this->_user->id, $bizRule);
            return true;
        } else {
            $this->addError('username','Error when attempting to assign this user to the project.'); 
            return false;
        }
        
    }
    
    /**
     * Generates an array of usernames to use for the autocomplete
     */
    public function createUsernameList()
    {
        $sql = "SELECT username FROM tbl_user";
        $command = Yii::app()->db->createCommand($sql);
        $rows = $command->queryAll();
        //format it for use with auto complete widget
        $usernames = array();
        foreach($rows as $row) {
            $usernames[]=$row['username'];
        }
        return $usernames;
    }
}
